﻿using System;
using System.Linq;
using System.Web;
using Umbraco.Core.Models;
using Umbraco.Web;

namespace nForum.BusinessLogic
{
    public static class UmbracoAppHelpers
    {
        private static readonly UmbracoHelper _umbracoHelper = new UmbracoHelper(UmbracoContext.Current);

        public static IPublishedContent CurrentForumPage()
        {
            var pageId = HttpContext.Current.Items["pageID"];
            if (pageId != null)
            {
                // For now just return the same page
                return GetPage(pageId.ToString());   
            }
            return null;
        }

        public static IPublishedContent GetPage(int id)
        {
            var key = string.Concat("node-", id);
            if (!HttpContext.Current.Items.Contains(key))
            {
                var node = _umbracoHelper.TypedContent(id);
                HttpContext.Current.Items.Add(key, node);
            }
            return HttpContext.Current.Items[key] as IPublishedContent;
        }

        public static IPublishedContent GetPage(string id)
        {
            return GetPage(Convert.ToInt32(id));
        }

        public static IPublishedContent GetForumRootNode(int? overideRoot = null)
        {
            const string key = "rootnode";
            if (!HttpContext.Current.Items.Contains(key))
            {
                IPublishedContent rootNode;
                if (overideRoot != null)
                {
                    rootNode = GetPage(Convert.ToInt32(overideRoot)).AncestorOrSelf(AppConstants.ForumDocType);
                    HttpContext.Current.Items.Add(key, rootNode);  
                }
                else
                {
                    var cPage = CurrentForumPage();
                    if (cPage != null)
                    {
                        rootNode = cPage.AncestorOrSelf(AppConstants.ForumDocType);
                        HttpContext.Current.Items.Add(key, rootNode);
                    }
                    else
                    {
                        return null;   
                    }
                }
            }
            return HttpContext.Current.Items[key] as IPublishedContent;
        }

        public static IPublishedContent GetLoginPage()
        {
            var cRoot = GetForumRootNode();
            if (cRoot != null)
            {
                const string key = "loginpage";
                if (!HttpContext.Current.Items.Contains(key))
                {
                    var node = cRoot.Descendants(AppConstants.NodeTypeLogin).FirstOrDefault();
                    HttpContext.Current.Items.Add(key, node);
                }
                return HttpContext.Current.Items[key] as IPublishedContent;
            }
            return null;
        }

        public static IPublishedContent GetRegisterPage()
        {
            var cRoot = GetForumRootNode();
            if (cRoot != null)
            {
                const string key = "registerpage";
                if (!HttpContext.Current.Items.Contains(key))
                {
                    var node = cRoot.Descendants(AppConstants.NodeTypeRegister).FirstOrDefault();
                    HttpContext.Current.Items.Add(key, node);
                }
                return HttpContext.Current.Items[key] as IPublishedContent;
            }
            return null;
        }

        public static IPublishedContent GetSearchPage()
        {
            var cRoot = GetForumRootNode();
            if (cRoot != null)
            {
                const string key = "searchpage";
                if (!HttpContext.Current.Items.Contains(key))
                {
                    var node = cRoot.Descendants(AppConstants.NodeTypeSearch).FirstOrDefault();
                    HttpContext.Current.Items.Add(key, node);
                }
                return HttpContext.Current.Items[key] as IPublishedContent;
            }
            return null;
        }
    }
}
